package cn.iocoder.yudao.module.ems.controller.admin.outbound;

import cn.iocoder.yudao.module.ems.controller.admin.outbound.vo.OutboundRespVO;
import cn.iocoder.yudao.module.ems.controller.admin.outbound.vo.OutboundInfoCreateReqVO;
import cn.iocoder.yudao.module.ems.controller.admin.outbound.vo.OutboundInfoRespVO;
import cn.iocoder.yudao.module.ems.convert.outbound.OutboundInfoConvert;
import cn.iocoder.yudao.module.ems.dal.dataobject.outbound.OutboundDO;
import cn.iocoder.yudao.module.ems.dal.dataobject.outbound.OutboundInfoDO;
import cn.iocoder.yudao.module.ems.service.outbound.OutboundInfoService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ems.controller.admin.outbound.vo.*;
import cn.iocoder.yudao.module.ems.dal.dataobject.outbound.OutboundDO;
import cn.iocoder.yudao.module.ems.convert.outbound.OutboundConvert;
import cn.iocoder.yudao.module.ems.service.outbound.OutboundService;

@Api(tags = "设备管理 - 出库记录")
@RestController
@RequestMapping("/ems/outbound")
@Validated
public class OutboundController {

    @Resource
    private OutboundService outboundService;

    @Resource
    private OutboundInfoService outboundInfoService;

    @PostMapping("/create")
    @ApiOperation("创建出库记录")
    @PreAuthorize("@ss.hasPermission('ems:outbound:create')")
    public CommonResult<Long> createOutbound(@Valid @RequestBody OutboundCreateReqVO createReqVO) {
        return success(outboundService.createOutbound(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新出库记录")
    @PreAuthorize("@ss.hasPermission('ems:outbound:update')")
    public CommonResult<Boolean> updateOutbound(@Valid @RequestBody OutboundUpdateReqVO updateReqVO) {
        outboundService.updateOutbound(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除出库记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('ems:outbound:delete')")
    public CommonResult<Boolean> deleteOutbound(@RequestParam("id") Long id) {
        outboundService.deleteOutbound(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得出库记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('ems:outbound:query')")
    public CommonResult<OutboundRespVO> getOutbound(@RequestParam("id") Long id) {
//        OutboundDO outbound = outboundService.getOutbound(id);
//        return success(OutboundConvert.INSTANCE.convert(outbound));
        OutboundDO outbound = outboundService.getOutbound(id);
        List<OutboundInfoDO> outboundInfoList = outboundInfoService.getOutboundInfoList(id);
        System.out.println(outboundInfoList);
        OutboundRespVO resp = new OutboundRespVO();
        resp.setId(outbound.getId());
        resp.setTime(outbound.getTime());
        resp.setNumber(outbound.getNumber());
        resp.setCreateTime(outbound.getCreateTime());
        resp.setRemarks(outbound.getRemarks());
        resp.setOutboundPerson(outbound.getOutboundPerson());
        resp.setStatus(outbound.getStatus());
        resp.setType(outbound.getType());
        resp.setReceiver(outbound.getReceiver());
        resp.setPickingDept(outbound.getPickingDept());
        resp.setOutboundInfo(OutboundInfoConvert.INSTANCE.convert(outboundInfoList));
        return success(resp);
    }

    @GetMapping("/list")
    @ApiOperation("获得出库记录列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('ems:outbound:query')")
    public CommonResult<List<OutboundRespVO>> getOutboundList(@RequestParam("ids") Collection<Long> ids) {
        List<OutboundDO> list = outboundService.getOutboundList(ids);
        return success(OutboundConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得出库记录分页")
    @PreAuthorize("@ss.hasPermission('ems:outbound:query')")
    public CommonResult<PageResult<OutboundRespVO>> getOutboundPage(@Valid OutboundPageReqVO pageVO) {
        PageResult<OutboundDO> pageResult = outboundService.getOutboundPage(pageVO);
        return success(OutboundConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出出库记录 Excel")
    @PreAuthorize("@ss.hasPermission('ems:outbound:export')")
    @OperateLog(type = EXPORT)
    public void exportOutboundExcel(@Valid OutboundExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<OutboundDO> list = outboundService.getOutboundList(exportReqVO);
        // 导出 Excel
        List<OutboundExcelVO> datas = OutboundConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "出库记录.xls", "数据", OutboundExcelVO.class, datas);
    }

    @DeleteMapping("/deleteInfo")
    @ApiOperation("删除出库信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('ems:outbound:delete')")
    public CommonResult<Boolean> deleteOutboundInfo(@RequestParam("id") Long id) {
        outboundInfoService.deleteOutboundInfo(id);
        return success(true);
    }

    @PostMapping("/insertBatchInfo")
    @ApiOperation("批量创建出库信息")
    @PreAuthorize("@ss.hasPermission('ems:outbound:create')")
    public CommonResult<Boolean> insertBatchInfo(@Valid @RequestBody Collection<OutboundInfoCreateReqVO> infoList) {
        outboundInfoService.insertBatchInfo(infoList);
        return success(true);
    }

    @PutMapping("/updateInfo")
    @ApiOperation("更新出库信息")
    @PreAuthorize("@ss.hasPermission('ems:outbound:update')")
    public CommonResult<Boolean> updateOutboundInfo(@Valid @RequestBody OutboundInfoRespVO updateReqVO) {
        outboundInfoService.updateOutboundInfo(updateReqVO);
        return success(true);
    }

}
